﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using WomanMotion.WebApi.Api.Utility;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Infrastructure.UnifyResult;
using WomanMotion.WebApi.Model.Dtos;
using WomanMotion.WebApi.Model.Dtos.Classify;
using WomanMotion.WebApi.Model.Dtos.Team;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Service;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Api.Controllers
{
    /// <summary>
    /// 队伍管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [XSSFilter]
    [ApiController]
    [DescriptionName("队伍管理", true, 40)]
    public class TeamController : BaseController
    {
        private readonly ITeamService _team;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="team"></param>
        public TeamController(ILogger<TeamController> logger, ITeamService team) : base(logger)
        {
            this._team = team;
        }

        /// <summary>
        /// 创建队伍
        /// </summary>
        /// <param name="dto">DTO</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<TeamVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("创建队伍", true, "队伍管理", "List", "TeamList")]
        public async Task<IResultOutput> Created(CreatedTeamDTO dto)
        {
            var res = await _team.CreatedAsync(dto);
            if (res != null)
            {
                return ResultOutput.Ok(res, "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 启用队伍
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("启用队伍")]
        public async Task<IResultOutput> Enable(PrimaryKeyDTO ids)
        {
            var res = await _team.UpdateDisableAsync(DisableEnum.Enable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 禁用队伍
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate), (int)HttpStatusCode.OK)]
        [DescriptionName("禁用队伍")]
        public async Task<IResultOutput> Disable(PrimaryKeyDTO ids)
        {
            var res = await _team.UpdateDisableAsync(DisableEnum.Disable, ids.Ids);
            if (res > 0)
            {
                return ResultOutput.Ok(new object(), "操作成功");
            }
            else
            {
                return ResultOutput.NotOk("操作失败");
            }
        }

        /// <summary>
        /// 更新队伍
        /// </summary>
        /// <param name="dto">dto</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<TeamVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("更新队伍")]
        public async Task<IResultOutput> Update(UpdateTeamDTO dto)
        {
            var res = await _team.UpdateAsync(dto);
            if (res == null)
            {
                return ResultOutput.NotOk("操作失败");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取队伍信息
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<TeamVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取队伍信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetById(long id)
        {
            var res = await _team.GetByIdAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 根据ID获取多表连接队伍信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<TeamVM>), (int)HttpStatusCode.OK)]
        [DescriptionName("根据ID获取多表连接队伍信息")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetCommonById(long id)
        {
            var res = await _team.GetByIDNavAsync(id);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 分页获取队伍数据
        /// </summary>
        /// <param name="pars">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(SwaggerResultTemplate<PageViewBase<List<TeamVM>>>), (int)HttpStatusCode.OK)]
        [DescriptionName("分页获取队伍数据")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetPageList(QueryParameterBase pars)
        {
            var res = await _team.GetPageNavListAsync(pars);
            if (res.Data == null || res.Data.Count == 0)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }

        /// <summary>
        /// 获取特定分类下的所有队伍
        /// </summary>
        /// <param name="classid">队伍ID</param>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(SwaggerResultTemplate<List<TeamVM>>), (int)HttpStatusCode.OK)]
        [DescriptionName("获取特定分类下的所有队伍")]
        [AllowAnonymous]
        public async Task<IResultOutput> GetList(long classid)
        {
            var res = await _team.GetListAsync(m => m.Ifdisable == 1 && m.Classid == classid);
            if (res == null)
            {
                return ResultOutput.NotOk("没有数据");
            }
            return ResultOutput.Ok(res, "操作成功");
        }
    }
}